package com.org.gundutubruk;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainMenuActivity extends Activity {
	/** Atribut */
	private Button tombolMain;
	private Button tombolSound;
	private Button tombolCredit;
	private Button tombolShowPlayer;
	private AnimationDrawable titleAnimation;
	private ImageView title;

	
	@Override
	public void onWindowFocusChanged (boolean hasFocus){
    	titleAnimation.start();
    }

	public void nextAct2(){
		this.startActivity(new Intent("android.intent.action.GAMEMODE"));
		overridePendingTransition(R.anim.fade, R.anim.hold);		
		this.finish();			
	}
	
	public void nextAct3(){
		this.startActivity(new Intent("android.intent.action.CREDIT"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();		
	}
	public void nextAct4(){
		this.startActivity(new Intent("android.intent.action.SHOWPLAYER"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();		
	}
	
	@Override
	public void onBackPressed(){
		GameSound.stopMenuMusic();
		Utility.serializePlayerHolder(this);//serialisasi sebelom finish main menu buat keluar game
		this.finish();		
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	setContentView(R.layout.main_menu_layout);	
    	/** Isi atribut tombol dengan yang ada di layout XML*/
		if(Utility.checkPlayerHolderAlreadyExist(this)) {
			Utility.deserializePlayerHolder(this);
		}
		title=(ImageView)findViewById(R.id.judul);
    	titleAnimation=(AnimationDrawable)title.getDrawable();
    	tombolMain=(Button)findViewById(R.id.tombolMain);
    	tombolSound=(Button)findViewById(R.id.tombolSound);
    	tombolShowPlayer=(Button)findViewById(R.id.tombolShowPlayer);
    	
    	if(GameSettingHolder.soundenabled)
    		tombolSound.setBackgroundResource(R.drawable.sound_button);
    	else
    		tombolSound.setBackgroundResource(R.drawable.sound_button_inactive);
    	
    	if(GameSettingHolder.soundenabled&&!GameSound.isMusicPlayed){
    		GameSound.startMenuMusic(this);
    	}
    	
    	tombolCredit=(Button)findViewById(R.id.tombolCredit);
    	/** Pasang aksi untuk masing-masing tombol */
    	tombolMain.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {				
				if(arg1.getAction()==MotionEvent.ACTION_DOWN){
					tombolMain.setBackgroundResource(R.drawable.main_button2_push);
				}else if(arg1.getAction()==MotionEvent.ACTION_UP){
					tombolMain.setBackgroundResource(R.drawable.main_button2);
					nextAct2();
				}
				return false;
			}
		});
    	tombolSound.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) { 
				Context context = getApplicationContext();
				CharSequence text;
				int duration = Toast.LENGTH_SHORT;
				Toast toast;
				if(GameSettingHolder.soundenabled){
					tombolSound.setBackgroundResource(R.drawable.sound_button_inactive);
					text = "Suara dimatikan";
					GameSettingHolder.soundenabled=false;
					GameSound.stopMenuMusic();
				}else{
					tombolSound.setBackgroundResource(R.drawable.sound_button);
					text = "Suara dinyalakan";
					GameSettingHolder.soundenabled=true;
					GameSound.startMenuMusic(getApplicationContext());
				}
				toast = Toast.makeText(context, text, duration);
				toast.show();
			}
		});
    	tombolCredit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				nextAct3();
			}
		});
    	tombolShowPlayer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				nextAct4();
			}
		});
    }
}